package com.fosu.dao;


import com.fosu.domain.Comment;
import com.fosu.vo.MiniProduct;
import org.apache.ibatis.annotations.*;
import org.joda.time.DateTime;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;


/**
 * Desription
 * 评论类持久层
 * @author manno
 * @date 2020/10/15
 */
@Mapper
@Repository
public interface CommentDao
{
    /**根据商品id获得该商品下的所有评论与评论的回复
     * @param currentPage 当前页数
     * @param pageSize 页大小
     * @param productId 商品id
     * @return List<Comment> 评论对象集合
     * */
    @Select("select comment.*,user.nick_name,user.avatar_url " +
            "from comment,user " +
            "where product_id=#{productId} " +
            "and target_comment_id=-1 " +
            "and comment.owner_user_id=user.user_id "+
            "ORDER BY comment.like DESC Limit #{currentPage},#{pageSize};")
            @Results(id="commentMap",value={
            @Result(property = "commentId",column = "comment_id"),
            @Result(property="productId",column="product_id"),
            @Result(property="ownerUserId",column="owner_user_id"),
            @Result(property="targetCommentId",column="target_comment_id"),
            @Result(property = "nickName",column = "nick_name"),
            @Result(property = "avatarUrl",column = "avatar_url"),
            @Result(property="content",column="content"),
            @Result(property="createTime",column="create_time"),
            @Result(property = "like",column = "like"),
    })
    List<Comment> findProductComments(@Param("currentPage")int currentPage,@Param("pageSize") int pageSize,@Param("productId") int productId);

    /**根据评论id获得该评论下的所有回复
     * @param commentId 评论id
     * @return List<Comment> 该评论下所有回复对象的集合
     * */
    @Select("select comment.*,user.nick_name,user.avatar_url " +
            "from comment,user " +
            "where target_comment_id=#{commentId} " +
            "and comment.owner_user_id=user.user_id;"
            )
    @ResultMap(value = {"commentMap"})
    List<Comment> findReplies(int commentId);

    /**
     * 查找是否已点赞评论
     * @param userId 用户id
     * @param commentId 评论id
     * @return Integer 1表示已点赞过,0表示未点赞过
     * */
    @Select("SELECT COUNT(*) FROM favorite_comment " +
            "WHERE user_id=#{userId} and comment_id=#{commentId}")
    Integer findIsFavorite(@Param("userId")Integer userId,@Param("commentId") Integer commentId);

    /**
     * 移除点赞
     * @param userId 用户id
     * @param commentId 要移除点赞的评论id
     * */
    @Delete("DELETE FROM favorite_comment " +
            "WHERE user_id=#{userId} and comment_id=#{commentId}")
    void removeFavorite(@Param("userId")Integer userId, @Param("commentId")Integer commentId);

    /**
     * 点赞
     * @param userId 用户id
     * @param commentId 被点赞的评论id
     * */
    @Delete("INSERT INTO favorite_comment(user_id,comment_id) " +
            "VALUES(#{userId},#{commentId})")
    void addFavorite(@Param("userId")Integer userId,@Param("commentId") Integer commentId);

    /**
     * 添加被点赞量
     * @param commentId 被点赞的评论id
     * */
    @Update("UPDATE comment " +
            "SET `LIKE` = `LIKE` + 1 " +
            "WHERE " +
            "`comment_id` = #{commnetId}")
    void addFavoriteCount(@Param("commentId")Integer commentId);

    /**
     * 减少被点赞量
     * @param commentId 要移除点赞的评论id
     * */
    @Update("UPDATE comment " +
            "SET `LIKE` = `LIKE` - 1 " +
            "WHERE " +
            "`comment_id` = #{commnetId} " +
            "and `LIKE` > 0")
    void decFavoriteCount(@Param("commentId")Integer commentId);

    /**
     * 发表评论
     * @param comment 评论类对象
     */
    @Insert("INSERT INTO "+
            "comment(product_id,owner_user_id,target_comment_id,content)"+
            "VALUES"+
            "(#{productId},#{ownerUserId},#{targetCommentId},#{content})")
    @ResultMap(value = {"commentMap"})
    void saveComment(Comment comment);


    /**
     * 根据用户id获取回复我的信息
     * @param userId
     * @return
     */
    @ResultMap(value = {"commentMap"})
    @Select("select * from  comment where comment_id in (select comment_id from favorite_comment where user_id =#{userId})")
    List<Comment> getFavoriteCommentByUserId(@Param("userId")int userId);


    /**
     * 获取最新回复我的时间
     * @param userId
     * @return
     */

    @Select("select create_time from comment WHERE target_comment_id in (SELECT target_comment_id from comment WHERE comment_id in (SELECT comment_id from comment WHERE owner_user_id =#{userId}))  order by `create_time`  desc limit 1")
    Date getLastCommentTime(Integer userId);


    /**
     * 获取回复我的总数
     * @param userId
     * @return
     */
    @Select("SELECT count(*) FROM comment c where target_comment_id in (SELECT comment_id FROM `comment` WHERE owner_user_id =#{userId}) " +
            " and owner_user_id != #{userId}")
    Integer getReplyToMECount(Integer userId);

    /**
     * 获取新增回复我的个数
     * @param lastCommentTime
     * @return
     */
    @Select("SELECT count(*) FROM comment where #{lastCommentTime}<create_time and create_time< #{nowTime}" +
            "and owner_user_id=#{userId}")
    Integer getRecentlyReplyToMECount(String lastCommentTime,String nowTime,Integer userId);

}